package com.xxxt.jxj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.javassist.bytecode.CodeIterator.Gap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.xdevapi.JsonArray;
import com.xxxt.entity.ClassGrade;
import com.xxxt.entity.Grade;
import com.xxxt.entity.Specialty;
import com.xxxt.entity.Student;
import com.xxxt.jxj.service.ClassGradeService;
import com.xxxt.jxj.service.GradeService;
import com.xxxt.jxj.service.SpecialtyService;
import com.xxxt.utils.UUIDUtil;
@Controller
@RequestMapping("/grade")
public class GradeController {
	@Resource
	private GradeService gradeService;
	@Resource
	private SpecialtyService specialtyService;
	@Resource
	private ClassGradeService classGradeService;
	/***
	 * 插入年级
	 * @param schoolId
	 * @param collegeId
	 * @param collegeName
	 * @return
	 */
	@RequestMapping(value="/insertGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject insertGrade(String specialtyId,String gradeName){
		JSONObject json = new JSONObject();
		try {
			Specialty specialty = specialtyService.selcetOneSpecialtyBySpecialtyId(specialtyId);
			if (specialty==null) {
				json.put("status", 2);
				json.put("message", "当前专业不存在,添加失败");
				return json;
			}				
			List<Grade> list = gradeService.selcetAllGradeBySpecialtyId(specialtyId);//学院对应的学校下的遍历
			Grade grade = new Grade();
			grade.setGradeId(UUIDUtil.getUUID());
			grade.setGrade(gradeName);
			grade.setSpecialty(specialty);
			for (Grade grade2 : list) {			
				if (grade2.getGrade()==gradeName||grade2.getGrade().equals(gradeName)) {
					json.put("status", -2);
					json.put("message", "当前专业已有相同的年级名称存在，请不要重复添加！");
					return json;			
				}
			}			
			boolean insertGrade = gradeService.insertGrade(grade);
			if (insertGrade) {
				json.put("status", 0);
				json.put("message", "添加成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "添加失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "添加失败");
			return json;
		}		
		return json;		
	}
	/**'
	 * 查询单个年级信息
	 * @param collegeId
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/selcetOneGradeByGradeId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetOneGradeByGradeId(String gradeId,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			Grade selcetOneGradeByGradeId = gradeService.selcetOneGradeByGradeId(gradeId);
			if (selcetOneGradeByGradeId!=null) {
				session.setAttribute("selcetOneGradeByGradeId", selcetOneGradeByGradeId);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("grade", selcetOneGradeByGradeId);			
			}else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;
		}	
		return json;				
	}
	/**
	 * 修改单个年级信息
	 * @param collegeId
	 * @param schoolId
	 * @param collegeName
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/updateGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateGrade(String specialtyId,String gradeName,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			Grade grade=(Grade) session.getAttribute("selcetOneGradeByGradeId");	

			Specialty specialty = specialtyService.selcetOneSpecialtyBySpecialtyId(specialtyId);
			if (specialty==null) {
				json.put("status", -1);
				json.put("message", "抱歉，当前列表中没有该专业！");
				return json;
			}
			List<Grade> selcetAllGradeBySpecialtyId = gradeService.selcetAllGradeBySpecialtyId(specialtyId);
			for (Grade grade2 : selcetAllGradeBySpecialtyId) {				
				if (grade2.getGrade()==gradeName||grade2.getGrade().equals(gradeName)) {
					json.put("status", -3);
					json.put("message", "抱歉，该专业已经有相同的年级名称咯！");
					return json;
				}
			}												
			boolean updateGrade = gradeService.updateGrade(gradeName, specialty.getSpecialtyId(), grade.getGradeId());
			if (updateGrade) {
				json.put("status", 0);
				json.put("message", "修改成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "修改失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "修改失败");
			return json;
		}		
	
		
		return json;
		}	
	/**
	 * 删除单个年级信息
	 * @param collegeId
	 * @return
	 */
	@RequestMapping(value="/deleteGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteGrade(String gradeId){
		JSONObject json = new JSONObject();
		try {
			boolean deleteGrade = gradeService.deleteGrade(gradeId);
			if (deleteGrade) {
				json.put("status", 0);
				json.put("message", "删除成功");
			}
			else {
				json.put("status", -1);
				json.put("message", "删除失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "删除失败");
			return json;
		}		
		return json;
	}
	/**
	 * 根据专业id 分页查询年级信息
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selcetAllPageGradeBySpecialtyId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetAllPageGradeBySpecialtyId(String specialtyId,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<Grade> selcetAllPageGradeBySpecialtyId = gradeService.selcetAllPageGradeBySpecialtyId(specialtyId, pageNum, pageSize);
			if (selcetAllPageGradeBySpecialtyId.size()>0) {
				List<ClassGrade> selcetAllPageClassGradeByGradeId=null;
								 
				
				String gradeName=null;
				
				String classGradeName=null;
			
				String classGradeId=null;
								
				List<String> arrayList = new ArrayList<>();
				
				List<String> arrayList1 = new ArrayList<>();//存classgradeid
				
			
				
			for (Grade grade : selcetAllPageGradeBySpecialtyId) {
				
				selcetAllPageClassGradeByGradeId = classGradeService.selcetAllPageClassGradeByGradeId(grade.getGradeId(), pageNum, pageSize);	
				
				gradeName=grade.getGrade();
				
				
				for (ClassGrade classGrade : selcetAllPageClassGradeByGradeId) {	
					 classGradeName = classGrade.getClassGrade();
					 
					 classGradeId = classGrade.getClassGradeId();
					 			 
					 arrayList.add(gradeName+classGradeName);
					 
					 arrayList1.add(classGradeId);
					 
					 json.put("gradeNameAndClassGradeName", arrayList);
					 
					 json.put("classGradeId", arrayList1);
					
				}
			
			}																				
				json.put("status", 0);
				json.put("message", "查询成功");
				
			}
			else {
				json.put("status", -2);
				json.put("message", "当前专业下年级没数据");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
		}
		return json;
	}
	
	/**
	 * 根据老师和年级查询出对应学生分页
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selectStudentByTnoAndGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selectStudentByTnoAndGrade(String gradeId,String tno,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {			
			List<ClassGrade> selectAllClassGradeByGradeId = classGradeService.selectAllClassGradeByGradeId(gradeId);
			if (selectAllClassGradeByGradeId.size()<=0) {
				json.put("status", -3);
				json.put("message", "当前对应班级没有你的学生");				
			}
			PageHelper.startPage(pageNum, pageSize);
			List<Student> selectStudentByTnoAndList = classGradeService.selectStudentByTnoAndList(tno, selectAllClassGradeByGradeId,pageNum,pageSize);
			if (selectStudentByTnoAndList.size()>0) {
				PageInfo<Student> pageInfo=new PageInfo<>(selectStudentByTnoAndList);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else if (selectStudentByTnoAndList.size()==0) {
				json.put("status", -2);
				json.put("message", "当前没数据");
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
		}
		return json;
	}
}
